package learn.basic;

import org.gephi.data.attributes.api.AttributeColumn;
import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Node;
import org.gephi.io.generator.plugin.RandomGraph;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.ContainerFactory;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.gephi.statistics.plugin.GraphDistance;
import org.openide.util.Lookup;

public class LayoutGraph {

	public static void main(String[] args) {
		ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
		pc.newProject();
		Workspace workspace = pc.getCurrentWorkspace();
		
		Container container = Lookup.getDefault().lookup(ContainerFactory.class).newContainer();
		RandomGraph randomGraph = new RandomGraph();
		randomGraph.setNumberOfNodes(500);
		randomGraph.setWiringProbability(0.005);
		randomGraph.generate(container.getLoader());
		
		ImportController importController = Lookup.getDefault().lookup(ImportController.class);
		importController.process(container, new DefaultProcessor(), workspace);
		
		GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
		AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
		DirectedGraph graph = graphModel.getDirectedGraph();
		System.out.println("Nodes: " + graph.getNodeCount());
		System.out.println("Edges: " + graph.getEdgeCount());

		GraphDistance distance = new GraphDistance();
		distance.setDirected(true);
		distance.execute(graphModel, attributeModel);
		
		AttributeColumn col = attributeModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS);
		for (Node n : graph.getNodes()) {
			Double centrality = (Double) n.getNodeData().getAttributes().getValue(col.getIndex());
			System.out.println("Centrality of node" + n.getId() + ": " + centrality);
		}
		
//		AutoLayout autoLayout = new AutoLayout(1, TimeUnit.MINUTES);
//		autoLayout.setGraphModel(graphModel);
//		YifanHuLayout firstLayout = new YifanHuLayout(null, new StepDisplacement(1f));
//		ForceAtlasLayout secondLayout = new ForceAtlasLayout(null);
//		DynamicProperty adjustBySizeProperty = AutoLayout.createDynamicProperty("forceAtlas.adjustSizes.name", Boolean.TRUE, 0.1f);
//		DynamicProperty repulsionProperty = AutoLayout.createDynamicProperty("forceAtlas.repulsionStrength.name", new Double(500.0), 0f);
//		autoLayout.addLayout(firstLayout, 0.5f);
//		autoLayout.addLayout(secondLayout, 0.5f, new DynamicProperty[] {adjustBySizeProperty, repulsionProperty});
//		autoLayout.execute();
		
//		ExportController ec = Lookup.getDefault().lookup(ExportController.class);
//		try {
//			ec.exportFile(new File("D://random_graph.pdf"));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return;
//		}
	}

}